package com.example.demo.config.websocket.controller;

import com.alibaba.fastjson.JSON;
import com.example.demo.config.websocket.util.SpringUtil;
import com.example.demo.outstyle.EchartData;
import com.example.demo.outstyle.Message;
import com.example.demo.service.PositionService;
import com.example.demo.service.serviceImp.PositionServiceImp;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
@ServerEndpoint("/websocket/{pageCode}")
public class WebSocketController {

    private static PositionService positionServiceImp = SpringUtil.getBean(PositionServiceImp.class);

    private static final String loggerName = WebSocketController.class.getName();

    public static Map<String, List<Session>> electricSocketmapp = new ConcurrentHashMap<String, List<Session>>();

    //建立连接
    @OnOpen
    public void onOpen(@PathParam("pageCode") String pageCode, Session session) {
        List<Session> sessions = electricSocketmapp.get(pageCode);
        if (sessions == null) {
            //创建集合会话
            List<Session> sessionsList = new ArrayList<>();
            sessionsList.add(session);
            electricSocketmapp.put(pageCode, sessionsList);
        } else {
            sessions.add(session);
        }
    }

   @Scheduled(cron = "0/5 * * * * *")
    public void sendMessageEchart() throws Exception{
        if (electricSocketmapp.isEmpty()) return ;
       boolean flag = true;
       for (Map.Entry<String, List<Session>> entry : electricSocketmapp.entrySet()) {
           if (entry.getValue().size() != 0) {
               flag = false;
               break;
           }
       }
       if (flag) return ;

        Message message = new Message();
        message.setMsg("查询成功");
        message.setState(0);

        EchartData echartData = new EchartData();
        //echartData.setCompanyLogo(positionServiceImp.selectCompanyLogo());

        echartData.setBase(positionServiceImp.selectBaseData());
        echartData.setCompanyBySize(positionServiceImp.selectCompanyBySize());
        echartData.setAdvantageWords(positionServiceImp.selectComponyWelfare());
        echartData.setLanguageCounts(positionServiceImp.selectLanguageCounts());
        echartData.setPositionByCity(positionServiceImp.selectPositionByCity());
        echartData.setPositionByEducation(positionServiceImp.selectPositionByEducation());
        echartData.setPositionBySalary(positionServiceImp.selectPositionBySalary());
        echartData.setSalaryByWorkYear(positionServiceImp.selectSalaryByWorkYear());
        echartData.setPositionByLanguage(positionServiceImp.selectPositionByLanguage());
        echartData.setTop5ByDistrict(positionServiceImp.selectTop5ByDistrict());
        echartData.setByFamous(positionServiceImp.selectByFamous());

        message.setData(echartData);

        int cnt = 0;
        String Json = JSON.toJSONString(message);
        for (Map.Entry<String, List<Session>> entry : electricSocketmapp.entrySet()) {
            List<Session> sessions = entry.getValue();
            for (Session session : sessions) {
                session.getBasicRemote().sendText(Json);
                cnt++;
            }
        }
        System.out.println("当前连接数: " + cnt);
        System.out.println("传送的数据: " + Json);
    }

    //收到调用的信息
    @OnMessage
    public void onMessage(String message, Session session) {
        System.out.println("接收到的信息: " + message);

        try {
            session.getBasicRemote().sendText("我推送的信息是" + message);
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

    @OnError
    public void onError(Session session, Throwable throwable) {
        System.out.println("发生错误");
    }

    @OnClose
    public void onClose(@PathParam("pageCode")String pageCode, Session session) {
        if (electricSocketmapp.containsKey(pageCode)) {
            electricSocketmapp.get(pageCode).remove(session);
        }
    }

}


